home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint108s.zoo / memprot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-27  |  31.4 KB  |  1,133 lines

  1. /*
  2.  * Copyright 1991,1992,1993 Atari Corporation.
  3.  * All rights reserved.
  4.  */
  5.  
  6. /*
  7.  * page-table data structures
  8.  *
  9.  *
  10.  * The root pointer points to a list of pointers to top-level pointer tables.
  11.  * 
  12.  * Each entry in a pointer table points to another pointer table or to
  13.  * a page table, or is a page descriptor.
  14.  * 
  15.  * Since, initially, the logical address space is the physical address space,
  16.  * we only need to worry about 26MB plus 32K for I/O space.
  17.  * 
  18.  * Since we want some pages to be supervisor-accessible, but we don't want
  19.  * a whole separate table for that, we use long-format descriptors.
  20.  * 
  21.  * Initial memory map:
  22.  * 
  23.  * 0     - membot: S (supervisor only)
  24.  * membot     - memtop: P (protected TPA)
  25.  * memtop     - phystop: G (screen)
  26.  * phystop     - $00E00000: bus error
  27.  * $00E00000- $00E3FFFF: G (ROM)
  28.  * $00E40000- $00FF7FFF: bus error
  29.  * $00FF8000- $00FFFFFF: G (mostly S: I/O space, but that's done in hardware)
  30.  * $01000000- ramtop: P
  31.  * ramtop     - $7FFFFFFF: G (A32/D32 VME, cacheable)
  32.  * $80000000- $FEFFFFFF: G (A32/D32 VME, non cacheable)
  33.  * $FFxxxxxx          just like $00xxxxxx.
  34.  * 
  35.  * Here's a final choice of layouts: IS=0, PS=13 (8K), TIA=4, TIB=4, TIC=4,
  36.  * TID=7.  This lets us map out entire unused megabytes at level C, and gives
  37.  * us an 8K page size, which is the largest the '040 can deal with.
  38.  * 
  39.  * This code implements 4+4+4+7, as follows:
  40.  * 
  41.  * tbl_a
  42.  *     0 -> tbl_b0
  43.  *     1-7 -> Cacheable direct (VME) page descriptors
  44.  *     8-E -> Non-cacheable direct (VME) page descriptors
  45.  *     F -> tbl_bf
  46.  * 
  47.  * tbl_b0 table: 16 entries (assumes only 16MB of TT RAM)
  48.  *     0 -> tbl_c00 (16MB of ST RAM address space)
  49.  *     1 -> tbl_c01 (16MB of TT RAM address space)
  50.  *     2-F -> cacheable direct (VME) page descriptors
  51.  * 
  52.  * tbl_bF table: 16 entries (deals with $FF mapping to $00)
  53.  *     0-E -> Non-cacheable direct (VME) page descriptors
  54.  *     F -> tbl_c00 (16MB of ST RAM address space, repeated here as $FF)
  55.  * 
  56.  * tbl_c00 table: ST RAM address space (example assuming 4MB ST RAM)
  57.  *     0-3 -> RAM page tables
  58.  *     4-D -> invalid
  59.  *     E -> direct map, cache enable (ROM)
  60.  *     F -> direct map, cache inhibit (I/O)
  61.  * 
  62.  * For each 16MB containing any TT RAM, there's a tbl_c.  Within those,
  63.  * for each MB that actually has TT RAM, there's another table, containing
  64.  * 128 RAM page tables.  Where there isn't RAM, there are "global"
  65.  * pages, to let the hardware bus error or not as it sees fit.
  66.  * 
  67.  * One RAM page table is allocated per megabyte of real RAM; each table has
  68.  * 128 entries, which is 8K per page.  For a TT with 4MB ST RAM and 4MB TT RAM
  69.  * that's 8K in page tables.  You can cut this down by not allocating page
  70.  * tables for which the entire megabyte is not accessible (i.e. it's all
  71.  * private memory and it's not YOUR private memory).
  72.  * 
  73.  * You have one of these per process.  When somebody loads into G or S memory
  74.  * or leaves it, you have to go through the page tables of every process
  75.  * updating S bits (for S) and DT (for G) bits.
  76.  * 
  77.  * The top levels are small & easy so replicating them once per process
  78.  * doesn't really hurt us.
  79.  * 
  80.  */
  81.  
  82. #include "mint.h"
  83.  
  84. #if 0
  85. #define MP_DEBUG(x) DEBUG(x)
  86. #else
  87. #define MP_DEBUG(x)
  88. #endif
  89.  
  90. void *memset P_((void *s, int ucharfill, unsigned long size));
  91. static void _dump_tree P_((long_desc tbl, int level));
  92.  
  93. extern int debug_level;        /* see debug.c */
  94. extern long mcpu;        /* in main.c */
  95.  
  96. /*
  97.  * You can turn this whole module off, and the stuff in context.s,
  98.  * by setting no_mem_prot to 1.
  99.  */
  100.  
  101. int no_mem_prot;
  102. long page_table_size;
  103.  
  104. /*
  105.  * PMMU stuff
  106.  */
  107.  
  108. /*
  109.  * This is one global TC register that is copied into every process'
  110.  * context, even though it never changes.  It's also used by the
  111.  * functions that dump page tables.
  112.  */
  113.  
  114. tc_reg tc;
  115.  
  116. /* mint_top_* get used in mem.c also */
  117. ulong mint_top_tt;
  118. ulong mint_top_st;
  119.  
  120. int tt_mbytes;        /* number of megabytds of TT RAM */
  121.  
  122. /*
  123.  * global_mode_table: one byte per page in the system.  Initially all pages
  124.  * are set to "global" but then the TPA pages are set to "invalid" in
  125.  * init_mem.  This has to be allocated and initialized in init_tables,
  126.  * when you know how much memory there is.  You need a byte per page,
  127.  * from zero to the end of TT RAM, including the space between STRAM
  128.  * and TTRAM.  That is, you need 16MB/pagesize plus (tt_mbytes/pagesize)
  129.  * bytes here.
  130.  */
  131.  
  132. unsigned char *global_mode_table;
  133.  
  134. /*
  135.  * prototype descriptors; field u1 must be all ones, other u? are zero.
  136.  * This is just the first long of a full descriptor; the ".page_type" part
  137.  * of the union.  These are initialized by init_tables.
  138.  *
  139.  * The proto_page_type table yields the value to stuff into the page_type
  140.  * field of a new process' page table.  It is the "non-owner" mode for
  141.  * a page with the corresponding value in global_mode_table.
  142.  */
  143.  
  144. page_type g_page;
  145. page_type g_ci_page;
  146. page_type s_page;
  147. page_type readable_page;
  148. page_type invalid_page;
  149. page_type page_ptr;
  150.  
  151. page_type *const proto_page_type[] =
  152.     { &invalid_page, &g_page, &s_page, &readable_page, &invalid_page };
  153. /*    private         global    super    private/read    invalid */
  154.  
  155. /*
  156.  * Init_tables: called sometime in initialization.  We set up some
  157.  * constants here, but that's all.  The first new_proc call will set up the
  158.  * page table for the root process and switch it in; from then on, we're
  159.  * always under some process' control.
  160.  * 
  161.  * The master page-mode table is initialized here, and some constants like
  162.  * the size needed for future page tables.
  163.  *
  164.  * One important constant initialized here is page_table_size, which is
  165.  * the amount of memory required per page table.  new_proc allocates
  166.  * this much memory for each process' page table.  This number will be
  167.  * 1K/megabyte plus page table overhead.  There are TBL_PAGES_OFFS
  168.  * tables at TBL_SIZE_BYTES each before the main tables begin; then
  169.  * there is 1024 bytes per megabyte of memory being mapped.
  170.  */
  171.  
  172. void
  173. init_tables()
  174. {
  175.     int n_megabytes;
  176.     long global_mode_table_size;
  177.  
  178.     if (no_mem_prot) return;
  179.  
  180.     TRACE(("init_tables"));
  181.  
  182. #define phys_top_tt (*(ulong *)0x5a4L)
  183.     if (phys_top_tt == 0x01000000L) mint_top_tt = 0;
  184.     else mint_top_tt = phys_top_tt;
  185.  
  186. #define phys_top_st (*(ulong *)0x42eL)
  187.     mint_top_st = phys_top_st;
  188.  
  189.     if (mint_top_tt)
  190.         tt_mbytes = (int) ((mint_top_tt - 0x01000000L) / ONE_MEG);
  191.     else
  192.         tt_mbytes = 0;
  193.  
  194.     n_megabytes = (int) ((mint_top_st / ONE_MEG) + tt_mbytes);
  195.  
  196.     /*
  197.      * page table size: room for A table, B0 table, BF table, STRAM C
  198.      * table, one TTRAM C table per 16MB (or fraction) of TTRAM, and 1024
  199.      * bytes per megabyte.
  200.      */
  201.  
  202.     page_table_size = (4L * TBL_SIZE_BYTES) +
  203.               (((tt_mbytes+15L)/16L) * TBL_SIZE_BYTES) +
  204.               (n_megabytes*1024L);
  205.  
  206.     global_mode_table_size = ((SIXTEEN_MEG / QUANTUM) +
  207.                 (((ulong)tt_mbytes * ONE_MEG) / QUANTUM));
  208.  
  209.     global_mode_table = kmalloc(global_mode_table_size);
  210.  
  211.     assert(global_mode_table);
  212.  
  213.     TRACELOW(("mint_top_st is $%lx; mint_top_tt is $%lx, n_megabytes is %d",
  214.     mint_top_st, mint_top_tt, n_megabytes));
  215.     TRACELOW(("page_table_size is %ld, global_mode_table_size %ld",
  216.         page_table_size,
  217.         global_mode_table_size));
  218.  
  219.     g_page.limit = 0x7fff;    /* set nonzero fields: disabled limit */
  220.     g_page.unused1 = 0x3f;    /* ones in this reserved field */
  221.     g_page.unused2 = 0;
  222.     g_page.s = 0;
  223.     g_page.unused3 = 0;
  224.     g_page.ci = 0;
  225.     g_page.unused4 = 0;
  226.     g_page.m = 1;        /* set m and u to 1 so CPU won't do writes */
  227.     g_page.u = 1;
  228.     g_page.wp = 0;        /* not write-protected */
  229.     g_page.dt = 1;        /* descriptor type 1: page descriptor */
  230.  
  231.     g_ci_page = g_page;
  232.     g_ci_page.ci = 1;
  233.  
  234.     readable_page = g_page;    /* a page which is globally readable */
  235.     readable_page.wp = 1;    /* but write protected */
  236.  
  237.     s_page = g_page;        /* a page which is globally accessible */
  238.     s_page.s = 1;        /* if you're supervisor */
  239.  
  240.     invalid_page = g_page;
  241.     invalid_page.dt = 0;
  242.  
  243.     page_ptr = g_page;
  244.     page_ptr.m = 0;        /* this must be zero in page pointers */
  245.     page_ptr.dt = 3;
  246.  
  247.     tc.enable = 1;
  248.     tc.zeros = 0;
  249.     tc.sre = 0;
  250.     tc.fcl = 0;
  251.     tc.is = 0;
  252.     tc.tia = 4;
  253.     tc.tib = 4;
  254.     tc.tic = 4;
  255.     tc.tid = 7;            /* 0+4+4+4+7+13 == 32 */
  256.     tc.ps = 13;            /* 8K page size */
  257.  
  258.     /* set the whole global_mode_table to "global" */
  259.     memset(global_mode_table,PROT_G,global_mode_table_size);
  260. }
  261.  
  262. /*
  263.  * mark_region: mark a region of memory as having a particular type.
  264.  * The arguments are the memory region in question and the new type.
  265.  * If the new type is zero then the old type is preserved.  The
  266.  * type of each page is kept in a global place for this purpose,
  267.  * among others.
  268.  *
  269.  * The types are:
  270.  *  0    private
  271.  *  1    global
  272.  *  2    private, but super-accessible
  273.  *  3    private, but world readable
  274.  *  4   invalid
  275.  *
  276.  
  277. The idea is this:
  278.  
  279.     for (each process) {
  280.     if (you're an owner or you're special) {
  281.         set up owner modes
  282.     }
  283.     else {
  284.         set up non-owner modes
  285.     }
  286.  
  287.     mark_pages(pagetbl,start,len,modes);
  288.     }
  289.  
  290.  */
  291.  
  292. /*
  293.                 invalid---v
  294.               private/gr---v  |
  295.                 super-------v  |  |
  296.             global-------v  |  |  |
  297.         private-------v     |  |  |  |
  298.                   |     |  |  |  |
  299. */
  300. const ushort other_dt[]    =   { 0, 1, 1, 1, 0 };
  301. const ushort other_s[] =    { 0, 0, 1, 0, 0 };
  302. const ushort other_wp[] =   { 0, 0, 0, 1, 0 };
  303.  
  304.  
  305. /*
  306.  * get_page_cookie: return a cookie representing the protection status
  307.  * of some memory.
  308.  *
  309.  * Returns ((wp << 3) | (s << 2) | (dt) | 0x8000) when it wins.
  310.  * Returns 1 if the pages are not all controlled, 0 if they're not all the same.
  311.  */
  312.  
  313. static short
  314. get_page_cookie(long_desc *base_tbl,ulong start,ulong len)
  315. {
  316.     int b_index, c_index, d_index;
  317.     long_desc *tbl;
  318.     int dt, s, wp;
  319.  
  320.     if (start < mint_top_st) {
  321.     /* start is in ST RAM; fail if not entirely in ST RAM */
  322.     if (start+len > mint_top_st) {
  323.         return 1;
  324.     }
  325.     }
  326.     else if (start >= 0x01000000L && start < mint_top_tt) {
  327.     /* start is in TT RAM; fail if not entirely in TT RAM */
  328.     if (start+len > mint_top_tt) {
  329.         return 1;
  330.     }
  331.     }
  332.  
  333.     b_index = (int)(start >> LOG2_16_MEG);
  334.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  335.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  336.  
  337.     tbl = &(base_tbl[0].
  338.         tbl_address[b_index].
  339.         tbl_address[c_index].
  340.             tbl_address[d_index]);
  341.  
  342.     dt = tbl->page_type.dt;
  343.     wp = tbl->page_type.wp;
  344.     s = tbl->page_type.s;
  345.  
  346.     /*
  347.      * This can be optimized: while in a single megabyte, a quick loop on
  348.      * successive long_desc's will work; similarly, while in a single 16MB,
  349.      * an outer loop on the same C-level table works.
  350.      */
  351.  
  352.     while (len) {
  353.     /*
  354.      * a_index is always zero.  Only the first 256MB is mapped.
  355.      * b_index is the 16MB number of the page.
  356.      * c_index is the 1MB number of that page within the 16MB (0-15)
  357.      * d_index is the 8K number within that 1MB (0-127).
  358.      */
  359.     b_index = (int)(start >> LOG2_16_MEG);
  360.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  361.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  362.  
  363.     tbl = base_tbl;            /* tbl := start of A table  */
  364.     tbl = tbl[0].tbl_address;    /*           B table  */
  365.     tbl = tbl[b_index].tbl_address;    /*           C table  */
  366.     tbl = tbl[c_index].tbl_address;    /*           D table  */
  367.     tbl = &tbl[d_index];        /* tbl := addr of page entry */
  368.  
  369.     if ((tbl->page_type.dt != dt) ||
  370.         (tbl->page_type.s != s) ||
  371.         (tbl->page_type.wp != wp)) {
  372.             /* fail because it's not all the same protection */
  373.         return 0;
  374.     }
  375.     len -= EIGHT_K;
  376.     start += EIGHT_K;
  377.     }
  378.     /* we passed -- all the pages in question have the same prot. status */
  379.     return (wp << 3) | (s << 2) | dt | 0x8000;
  380. }
  381.  
  382. static void
  383. mark_pages(long_desc *base_tbl,ulong start,ulong len,
  384.         ushort dt_val, ushort s_val, ushort wp_val, PROC *proc)
  385. {
  386.     int b_index, c_index, d_index;
  387.     long_desc *tbl;
  388.     ulong oldstart, oldlen;
  389.  
  390.     if (no_mem_prot) return;
  391.  
  392.     oldstart = start;
  393.     oldlen = len;
  394.  
  395. #ifdef MEMPROT_SHORTCUT
  396.     /*
  397.      * Take a shortcut here: we're done if first page of the region is
  398.      * already right. We duplicate the top of the "while" loop to do it,
  399.      * but what the heck.
  400.      */
  401.     /* I don't think this shortcut is a good idea, since while we
  402.      * are doing Mshrink or Srealloc we may very well have a region
  403.      * with mixed page types -- ERS
  404.      */
  405.  
  406.     b_index = (start >> LOG2_16_MEG);
  407.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  408.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  409.  
  410.     tbl = &(base_tbl[0].
  411.         tbl_address[b_index].
  412.         tbl_address[c_index].
  413.             tbl_address[d_index]);
  414.  
  415.     if (tbl->page_type.dt == dt_val &&
  416.     tbl->page_type.s == s_val &&
  417.     tbl->page_type.wp == wp_val) {
  418. /*
  419.         TRACE(("mark_pages a:0 b:%d c:%d d:%d (same)",
  420.             b_index,c_index,d_index));
  421. */
  422.         return;
  423.     }
  424.  
  425. #endif /* MEMPROT_SHORTCUT */
  426. /*
  427.     MP_DEBUG(("mark_pages a:0 b:%d c:%d d:%d (diff)",b_index,c_index,d_index));
  428. */
  429.  
  430.     /*
  431.      * This can be optimized: while in a single megabyte, a quick loop on
  432.      * successive long_desc's will work; similarly, while in a single 16MB,
  433.      * an outer loop on the same C-level table works.
  434.      */
  435.  
  436.     while (len) {
  437.     /*
  438.      * a_index is always zero.  Only the first 256MB is mapped.
  439.      * b_index is the 16MB number of the page.
  440.      * c_index is the 1MB number of that page within the 16MB (0-15)
  441.      * d_index is the 8K number within that 1MB (0-127).
  442.      */
  443.     b_index = (int)(start >> LOG2_16_MEG);
  444.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  445.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  446.  
  447.     tbl = base_tbl;            /* tbl := start of A table  */
  448.     tbl = tbl[0].tbl_address;    /*           B table  */
  449.     tbl = tbl[b_index].tbl_address;    /*           C table  */
  450.     tbl = tbl[c_index].tbl_address;    /*           D table  */
  451.     tbl = &tbl[d_index];        /* tbl := addr of page entry */
  452.  
  453.     tbl->page_type.dt = dt_val;
  454.     tbl->page_type.s = s_val;
  455.     tbl->page_type.wp = wp_val;
  456.     len -= EIGHT_K;
  457.     start += EIGHT_K;
  458.     }
  459.  
  460.     flush_pmmu();
  461.     if (mcpu <= 30) {
  462.     /* On the '020 & '030 we have a logical cache, i.e. the DC & IC are on
  463.      * the CPU side of the MMU, hence on an MMU context switch we must flush
  464.      * them too. On the '040, by comparison, we have a physical cache, i.e.
  465.      * the DC & IC are on the memory side of the MMU, so no DC/IC cache flush
  466.      * is needed.
  467.      */
  468.     cpush((void *)oldstart, oldlen);
  469.     }
  470. }
  471.  
  472. /* get_prot_mode(r): returns the type of protection region r
  473.  * has
  474.  */
  475.  
  476. int
  477. get_prot_mode(r)
  478.     MEMREGION *r;
  479. {
  480.     ulong start = r->loc;
  481.  
  482.     if (no_mem_prot)
  483.         return PROT_G;
  484.     return global_mode_table[(start >> 13)];
  485. }
  486.  
  487. void
  488. mark_region(region,mode)
  489. MEMREGION *region;
  490. short mode;
  491. {
  492.     ulong start = region->loc;
  493.     ulong len = region->len;
  494.     ulong i;
  495.     ushort dt_val, s_val, wp_val;
  496.     PROC *proc;
  497.     MEMREGION **mr;
  498.  
  499.     if (no_mem_prot) return;
  500.  
  501.     MP_DEBUG(("mark_region %lx len %lx mode %d",start,len,mode));
  502.     
  503.     if (mode == PROT_NOCHANGE) {
  504.     mode = global_mode_table[(start >> 13)];
  505.     }
  506.  
  507.     /* mark the global page table */
  508.  
  509.     memset(&global_mode_table[start >> 13],mode,(len >> 13));
  510.  
  511.     for (proc = proclist; proc; proc = proc->gl_next) {
  512.     assert(proc->page_table);
  513.     if (mode == PROT_I || mode == PROT_G) {
  514.         /* everybody gets the same flags */
  515.         goto notowner;
  516.     }
  517.     if (proc->memflags & F_OS_SPECIAL) {
  518.         /* you're special; you get owner flags */
  519.         MP_DEBUG(("mark_region: pid %d is an OS special!",proc->pid));
  520.         goto owner;
  521.     }
  522.     if ((mr = proc->mem) != 0) {
  523.         for (i = 0; i < proc->num_reg; i++, mr++) {
  524.         if (*mr == region) {
  525.             MP_DEBUG(("mark_region: pid %d is an owner",proc->pid));
  526. owner:
  527.             dt_val = 1;
  528.             s_val = 0;
  529.             wp_val = 0;
  530.             goto gotvals;
  531.         }
  532.         }
  533.     }
  534.  
  535. notowner:
  536.  
  537. /* if you get here you're not an owner, or mode is G or I */
  538.     MP_DEBUG(("mark_region: pid %d gets non-owner modes",proc->pid));
  539.  
  540.     dt_val = other_dt[mode];
  541.     s_val = other_s[mode];
  542.     wp_val = other_wp[mode];
  543.  
  544. gotvals:
  545.     mark_pages(proc->page_table,start,len,dt_val,s_val,wp_val,proc);
  546.     }
  547. }
  548.  
  549. /*
  550.  * prot_temp: temporarily alter curproc's access to memory.
  551.  * Pass in a -1 to give curproc global access; returns a cookie.  Call
  552.  * again with that cookie to return the memory to the old mode.
  553.  * There should be no context switches or memory protection changes
  554.  * in the meantime.
  555.  *
  556.  * If called with mode == -1, returns...
  557.  *    -1 if mem prot is off -- no error, no action.
  558.  *     0 if the pages are not all the same.
  559.  *     1 if the pages are not all controlled by the page tables.
  560.  *
  561.  * When mode != -1, returns...
  562.  *    0 for success (should never fail).  There is little checking.
  563.  * Calling with mode == 0 or 1 results in zero to spoof success, but in fact
  564.  * this is an error.  Mode is only really valid if (mode & 0x8000).
  565.  */
  566.  
  567. int
  568. prot_temp(loc,len,mode)
  569. ulong loc;
  570. ulong len;
  571. int mode;
  572. {
  573.     int cookie;
  574.  
  575.     if (no_mem_prot) return -1;
  576.  
  577.     /* round start down to the previous page and len up to the next one. */
  578.     loc &= ~MASKBITS;
  579.     len = ROUND(len);
  580.  
  581.     if (mode == 0 || mode == 1) return 0;    /* do nothing */
  582.     if (mode == -1) {
  583.     cookie = get_page_cookie(curproc->page_table,loc,len);
  584.  
  585.     /* if not all controlled, return status */
  586.     if (cookie == 0 || cookie == 1) return cookie;
  587.  
  588.     mark_pages(curproc->page_table,loc,len,1,0,0,curproc);
  589.  
  590.     return cookie;
  591.     }
  592.     else {
  593.     mark_pages(curproc->page_table,loc,len,
  594.             mode&3,(mode&4)>>2,(mode&8)>>3,curproc);
  595.     return 0;
  596.     }
  597. }
  598.  
  599. /*
  600.  * init_page_table: fill in the page table for the indicated process. The
  601.  * master page map is consulted for the modes of all pages, and the memory
  602.  * region data structures are consulted to see if this process is the owner
  603.  * of any of those tables.
  604.  *
  605.  * This also sets crp and tc in both ctxts of the process.  If this is the
  606.  * first call, then the CPU tc is cleared, the TT0 and TT1 regs are zapped,
  607.  * and then this proc's crp and tc are loaded into it.
  608.  */
  609.  
  610. static short mmu_is_set_up = 0;
  611.  
  612. void
  613. init_page_table(proc)
  614. PROC *proc;
  615. {
  616.     long_desc *tptr;
  617.     long_desc *tbl_a;        /* top-level table */
  618.     long_desc *tbl_b0;        /* second level, handles $0 nybble */
  619.     long_desc *tbl_bf;        /* handles $F nybble */
  620.     long_desc *tbl_c;        /* temp pointer to start of 16MB */
  621.     ulong p, q, r;
  622.     ulong i, j, k;
  623.     int g;
  624.     MEMREGION **mr;
  625.  
  626.     if (no_mem_prot) return;
  627.  
  628.     if (proc->pid)
  629.         TRACELOW(("init_page_table(proc=%lx, pid %d)",proc,proc->pid));
  630.  
  631.     assert(proc && proc->page_table);
  632.  
  633.     tptr = proc->page_table;
  634.     tbl_a = tptr;
  635.     tptr += TBL_SIZE;
  636.     tbl_b0 = tptr;
  637.     tptr += TBL_SIZE;
  638.     tbl_bf = tptr;
  639.     tptr += TBL_SIZE;
  640.  
  641.     /*
  642.      * table A indexes by the first nybble: $0 and $F refer to their tables,
  643.      * $1-$7 are uncontrolled, cacheable; $8-$E are uncontrolled, ci.
  644.      */
  645.  
  646.     tbl_a[0].page_type = page_ptr;
  647.     tbl_a[0].tbl_address = tbl_b0;
  648.  
  649.     for (i=1; i<0xf; i++) {
  650.     if (i < 8)  tbl_a[i].page_type = g_page;
  651.     else        tbl_a[i].page_type = g_ci_page;
  652.     tbl_a[i].tbl_address = (long_desc *)(i << 28);
  653.     }
  654.  
  655.     /* $F entry of table A refers to table BF */
  656.     tbl_a[0xf].page_type = page_ptr;
  657.     tbl_a[0xf].tbl_address = tbl_bf;
  658.  
  659.     /*
  660.      * table B0: entry 0 is $00, the 16MB of ST address space.
  661.      */
  662.  
  663.     tbl_b0[0].page_type = page_ptr;
  664.     tbl_b0[0].tbl_address = tptr;
  665.     tbl_c = tptr;
  666.     tptr += TBL_SIZE;
  667.  
  668.     /* for each megabyte that is RAM, allocate a table */
  669.     for (i = 0, k = 0, p = 0; p < mint_top_st; i++, p += 0x00100000L) {
  670.     tbl_c[i].page_type = page_ptr;
  671.     tbl_c[i].tbl_address = tptr;
  672.  
  673.     /* for each page in this megabyte, write a page entry */
  674.     for (q = p, j = 0; j < 128; j++, q += 0x2000, k++) {
  675.         tptr->page_type = *proto_page_type[global_mode_table[k]];
  676.         tptr->tbl_address = (long_desc *)q;
  677.         tptr++;
  678.     }
  679.     }
  680.  
  681.     /* now for each megabyte from mint_top_st to ROM, mark global */
  682.     for ( ; p < 0x00E00000L; i++, p += 0x00100000L) {
  683.     tbl_c[i].page_type = g_page;
  684.     tbl_c[i].tbl_address = (long_desc *)p;
  685.     }
  686.  
  687.     /* fill in the E and F tables: 00Ex is ROM, 00Fx is I/O  */
  688.     tbl_c[i].page_type = g_page;
  689.     tbl_c[i].tbl_address = (long_desc *)p;
  690.     i++, p += 0x00100000L;
  691.     tbl_c[i].page_type = g_ci_page;
  692.     tbl_c[i].tbl_address = (long_desc *)p;
  693.  
  694.         /* Done with tbl_c for 0th 16MB; go on to TT RAM */
  695.  
  696. /* 
  697.     structure:
  698.  
  699.     for (i = each 16MB that has any TT RAM in it)
  700.     allocate a table tbl_c, point tbl_b0[i] at it
  701.     for (j = each 1MB that is RAM)
  702.         allocate a table, point tbl_c[j] at it
  703.         for (k = each page in the megabyte)
  704.         fill in tbl_c[j][k] with page entry from global_mode_table
  705.     for (j = the rest of the 16MB)
  706.         set tbl_c[j] to "global, cacheable"
  707.  
  708.     for (i = the rest of the 16MBs from here to $7F)
  709.     set tbl_b0[i] to "global, cacheable"
  710.  
  711.     for (i = the rest of the 16MBs from $80 up to but not incl. $FF)
  712.     set tbl_b0[i] to "global, not cacheable"
  713. */
  714.  
  715.     /* i counts 16MBs */
  716.     for (i = 1, p = 0x01000000L, g = 2048;
  717.      p < mint_top_tt;
  718.      p += SIXTEEN_MEG, i++) {
  719.         tbl_b0[i].page_type = page_ptr;
  720.         tbl_b0[i].tbl_address = tptr;
  721.         tbl_c = tptr;
  722.         tptr += TBL_SIZE;
  723.  
  724.         /* j counts MBs */
  725.         for (j = 0, q = p; j < 16 && q < mint_top_tt; q += ONE_MEG, j++) {
  726.         tbl_c[j].page_type = page_ptr;
  727.         tbl_c[j].tbl_address = tptr;
  728.         /* k counts pages (8K) */
  729.         for (r = q, k = 0; k < 128; k++, r += 0x2000, g++) {
  730.             tptr->page_type = *proto_page_type[global_mode_table[g]];
  731.             tptr->tbl_address = (long_desc *)r;
  732.             tptr++;
  733.         }
  734.         }
  735.         for ( ; j < 16; j++, q += ONE_MEG) {
  736.         /* fill in the rest of this 16MB */
  737.         tbl_c[j].page_type = g_page;
  738.         tbl_c[j].tbl_address = (long_desc *)q;
  739.         }
  740.     }
  741.  
  742.     /* fill in the rest of $00-$0F as cacheable */
  743.     for ( ; i < 16; i++, p += SIXTEEN_MEG) {
  744.     tbl_b0[i].page_type = g_page;
  745.     tbl_b0[i].tbl_address = (long_desc *)p;
  746.     }
  747.  
  748.     /* done with TT RAM in table b0; do table bf */
  749.  
  750.     /*
  751.      * Table BF: translates addresses starting with $F.  First 15 are
  752.      * uncontrolled, cacheable; last one translates $FF, which
  753.      * which shadows $00 (the 16MB ST address space).  The rest
  754.      * are uncontrolled, not cacheable.
  755.      *
  756.      * The table address of the copy has a 1 in the low (unused) bit, which
  757.      * is a signal to the table dumper not to dump this, as it's a copy
  758.      * of tbl_b0[0].
  759.      */
  760.  
  761.     for (i=0; i<0xf; i++) {
  762.     tbl_bf[i].page_type = g_ci_page;
  763.     tbl_bf[i].tbl_address = (long_desc *)((i << 24) | 0xf0000000L);
  764.     }
  765.     tbl_bf[0xf] = tbl_b0[0];
  766.     *(ulong *)(&(tbl_bf[0xf].tbl_address)) |= 1;
  767.  
  768.     proc->ctxt[0].crp.limit = 0x7fff;    /* disable limit function */
  769.     proc->ctxt[0].crp.dt = 3;        /* points to valid 8-byte entries */
  770.     proc->ctxt[0].crp.tbl_address = tbl_a;
  771.     proc->ctxt[1].crp = proc->ctxt[0].crp;
  772.     proc->ctxt[0].tc = tc;
  773.     proc->ctxt[1].tc = tc;
  774.  
  775.     /*
  776.      * OK, memory tables are now there as if you're a non-owner of every
  777.      * page.  Now for each region you ARE an owner of, mark with owner
  778.      * modes.
  779.      */
  780.  
  781.     mr = proc->mem;
  782.     for (i=0; i < proc->num_reg; i++, mr++) {
  783.     if (*mr) {
  784.             mark_pages(proc->page_table,(*mr)->loc,(*mr)->len,1,0,0,proc);
  785.         }
  786.     }
  787.  
  788.     if (!mmu_is_set_up) {
  789.     set_mmu(proc->ctxt[0].crp,proc->ctxt[0].tc);
  790.     mmu_is_set_up = 1;
  791.     }
  792. }
  793.  
  794. /*
  795.  * This routine is called when procfs detects that a process wants to be an
  796.  * OS SPECIAL.  The AES, SCRENMGR, and DESKTOP do this, and so does FSMGDOS
  797.  * and possibly some other stuff. It has to re-mark every page in that
  798.  * process' page table based on its new special status. The "special
  799.  * status" is "you get global access to all of memory" and "everybody
  800.  * gets Super access to yours."  It is the caller's responsibility
  801.  * to set proc's memflags, usually to (F_OS_SPECIAL | F_PROT_S).
  802.  */
  803.  
  804. void
  805. mem_prot_special(proc)
  806. PROC *proc;
  807. {
  808.     MEMREGION **mr;
  809.     int i;
  810.  
  811.     if (no_mem_prot) return;
  812.  
  813.     TRACE(("mem_prot_special(pid %d)",proc->pid));
  814.  
  815.     /*
  816.      * This marks ALL memory, allocated or not, as accessible. When memory
  817.      * is freed even F_OS_SPECIAL processes lose access to it. So one or
  818.      * the other of these is a bug, depending on how you want it to work.
  819.      */
  820.     mark_pages(proc->page_table,0,mint_top_st,1,0,0,proc);
  821.     if (mint_top_tt) {
  822.     mark_pages(proc->page_table,
  823.             0x01000000L,
  824.             mint_top_tt - 0x01000000L,
  825.             1,0,0,
  826.             proc);
  827.     }
  828.  
  829.     /*
  830.      * In addition, mark all the pages the process already owns as "super"
  831.      * in all other processes.  Thus the "special" process can access all
  832.      * of memory, and any process can access the "special" process' memory
  833.      * when in super mode.
  834.      */
  835.  
  836.     mr = proc->mem;
  837.  
  838.     for (i=0; i < proc->num_reg; i++, mr++) {
  839.     if (*mr) {
  840.         mark_region(*mr,PROT_S);
  841.     }
  842.     }
  843. }
  844.     
  845. /*----------------------------------------------------------------------------
  846.  * DEBUGGING SECTION
  847.  *--------------------------------------------------------------------------*/
  848.  
  849. static void
  850. _dump_tree(tbl,level)
  851. long_desc tbl;
  852. int level;
  853. {
  854.     int i, j;
  855.     long_desc *p;
  856.     static const char spaces[9] = "        ";
  857.  
  858.     /* print the level and display the table descriptor */
  859.     FORCE("\r%s s:%x wp:%x dt:%x a:%08lx",
  860.     &spaces[8-(level*2)],
  861.     tbl.page_type.s,
  862.     tbl.page_type.wp,
  863.     tbl.page_type.dt,
  864.     tbl.tbl_address);
  865.  
  866.     if (tbl.page_type.dt == 3) {
  867.     if (level == 0) {
  868.         j = (1 << tc.tia);
  869.     }
  870.     else if (level == 1) {
  871.         j = (1 << tc.tib);
  872.     }
  873.     else if (level == 2) {
  874.         j = (1 << tc.tic);
  875.     }
  876.     else {
  877.         j = (1 << tc.tid);
  878.     }
  879.  
  880.     /* don't show table if it's the duplicate */
  881.     if ((ulong)tbl.tbl_address & 1) return;
  882.  
  883.     ++level;
  884.     p = tbl.tbl_address;
  885.     for (i=0; i<j; i++, p++) {
  886.         _dump_tree(*p,level);
  887.     }
  888.     }
  889. }
  890.  
  891. static const char modesym[] = { 'p', 'g', 's', 'r', 'i' };
  892.  
  893. void
  894. QUICKDUMP()
  895. {
  896.     char outstr[33];
  897.     ulong i, j, end;
  898.  
  899.     if (no_mem_prot) return;
  900.  
  901.     FORCE("STRAM global table:");
  902.     outstr[32] = '\0';
  903.     end = mint_top_st / QUANTUM;
  904.     for (i = 0; i < end; i += 32) {
  905.     for (j=0; j<32; j++) {
  906.         outstr[j] = modesym[global_mode_table[j+i]];
  907.     }
  908.     FORCE("%08lx: %s",i*8192L,outstr);
  909.     }
  910.  
  911.     if (mint_top_tt) {
  912.     FORCE("TTRAM global table:");
  913.     end = mint_top_tt / QUANTUM;
  914.     for (i = 2048; i < end; i += 32) {
  915.         for (j=0; j<32; j++) {
  916.         outstr[j] = modesym[global_mode_table[j+i]];
  917.         }
  918.         FORCE("%08lx: %s",i*8192L,outstr);
  919.     }
  920.     }
  921. }
  922.  
  923. const char *berr_msg[] = { 
  924. /*  "........." */
  925.     "private  ",
  926.     "global   ",    /* turned into "hardware" for violation reports */
  927.     "super    ",
  928.     "readable ",
  929.     "free     ",
  930.     "hardware "        /* used when the memory is not controlled by us */
  931. };
  932.  
  933. void
  934. report_buserr()
  935. {
  936.     const char *vmsg;
  937.     short mode;
  938.     ulong aa, pc;
  939.     char alertbuf[5*32+16];    /* enough for an alert */
  940.     char *aptr;
  941.  
  942.     if (no_mem_prot) return;
  943.  
  944.     aa = curproc->exception_addr;
  945.     pc = curproc->exception_pc;
  946.     if ((mint_top_tt && aa < mint_top_tt) || (aa < mint_top_st)) {
  947.     mode = global_mode_table[(curproc->exception_addr >> 13)];
  948.     if (mode == PROT_G) {
  949.         /* page is global: obviously a hardware bus error */
  950.         mode = 5;
  951.     }
  952.     }
  953.     else {
  954.     /* (addr is > mint_top_tt) set mode = 5 so we don't look for owners */
  955.         mode = 5;
  956.     }
  957.     vmsg = berr_msg[mode];
  958.  
  959.     /* construct an AES alert box for this error:
  960.     | PROCESS  "buserrxx"  KILLED: |
  961.     | MEMORY VIOLATION.  (PID 000) |
  962.     |                              |
  963.     | Type: ......... PC: pc...... |
  964.     | Addr: ........  BP: ........ |
  965.     */
  966.  
  967.     /* we play games to get around 128-char max for ksprintf */
  968.     ksprintf(alertbuf,"[1][ PROCESS  \"%s\"  KILLED: |",curproc->name);
  969.     aptr = alertbuf + strlen(alertbuf);
  970.     ksprintf(aptr," MEMORY VIOLATION.  (PID %03d) | |",curproc->pid);
  971.     aptr = alertbuf + strlen(alertbuf);
  972.     ksprintf(aptr," Type: %s PC: %08lx |",vmsg,pc);
  973.     aptr = alertbuf + strlen(alertbuf);
  974.     ksprintf(aptr," Addr: %08lx  BP: %08lx ][ OK ]",aa,curproc->base);
  975.     if (!_ALERT(alertbuf)) {
  976.         /* this will call _alert again, but it will just fail again */
  977.         ALERT("MEMORY VIOLATION: type=%s AA=%lx PC=%lx BP=%lx",
  978.             vmsg,aa,pc,curproc->base);
  979.     }
  980.         
  981.     if (curproc->pid == 0 || curproc->memflags & F_OS_SPECIAL) {
  982.     /* the system is so thoroughly hosed that anything we try will
  983.      * likely cause another bus error; so let's just hang up
  984.      */
  985.     FATAL("Operating system killed");
  986.     }
  987. }
  988.  
  989. /*
  990.  * big_mem_dump is a biggie: for each page in the system, it
  991.  * displays the PID of the (first) owner and the protection mode.
  992.  * The output has three chars per page, and eight chars per line.
  993.  * The first page of a region is marked with the mode, and the
  994.  * rest with a space.
  995.  *
  996.  * Logic:
  997.     for (mp = *core; mp; mp++) {
  998.     for (each page of this region) {
  999.         if (start of line) {
  1000.         output line starter;
  1001.         }
  1002.         if (start of region) {
  1003.         output mode of this page;
  1004.         determine owner;
  1005.         output owner;
  1006.         }
  1007.         else {
  1008.         output space;
  1009.         output owner;
  1010.         }
  1011.         }
  1012.     }
  1013.  */
  1014.  
  1015. void
  1016. BIG_MEM_DUMP(bigone,proc)
  1017. int bigone;
  1018. PROC *proc;
  1019. {
  1020. #ifdef DEBUG_INFO
  1021.     char linebuf[128];
  1022.     char *lp = linebuf;
  1023.     MEMREGION *mp, **mr, **map;
  1024.     PROC *p;
  1025.     ulong loc;
  1026.     short owner;
  1027.     short i;
  1028.     short first;
  1029.  
  1030.  
  1031.   if (no_mem_prot) return;
  1032.  
  1033.   for (map = core; map != 0; ((map == core) ? (map = alt) : (map = 0))) {
  1034.     FORCE("Annotated memory dump for %s",(map == core ? "core" : "alt"));
  1035.     first = 1;
  1036.     *linebuf = '\0';
  1037.     for (mp = *map; mp; mp = mp->next) {
  1038.     for (loc = mp->loc; loc < (mp->loc + mp->len); loc += EIGHT_K) {
  1039.         if (first || ((loc & 0x1ffff) == 0)) {
  1040.         if (*linebuf) FORCE(linebuf);
  1041.         ksprintf(linebuf,"\r%08lx: ",loc);
  1042.         lp = &linebuf[11];
  1043.         first = 0;
  1044.         }
  1045.         if (loc == mp->loc) {
  1046.         *lp++ = modesym[global_mode_table[loc / EIGHT_K]];
  1047.  
  1048.         for (p = proclist; p; p = p->gl_next) {
  1049.             if (p->mem) {
  1050.             mr = p->mem;
  1051.             for (i=0; i < p->num_reg; i++, mr++) {
  1052.                 if (*mr == mp) {
  1053.                 owner = p->pid;
  1054.                 goto gotowner;
  1055.                 }
  1056.             }
  1057.             }
  1058.         }
  1059.         owner = 000;
  1060. gotowner:
  1061.         ksprintf(lp,"%03d",owner);
  1062.         lp += 3;
  1063.         }
  1064.         else {
  1065.         *lp++ = ' ';
  1066.         *lp++ = '-';
  1067.         *lp++ = '-';
  1068.         *lp++ = '-';
  1069.         *lp = '\0';    /* string is always null-terminated */
  1070.         }
  1071.         }
  1072.     }
  1073.     FORCE(linebuf);
  1074.   }
  1075.  
  1076.     if (bigone) {
  1077.     long_desc tbl;
  1078.  
  1079.     /* fill in tbl with the only parts used at the top level */
  1080.     tbl.page_type.dt = proc->ctxt[CURRENT].crp.dt;
  1081.     tbl.tbl_address = proc->ctxt[CURRENT].crp.tbl_address;
  1082.     _dump_tree(tbl,0);
  1083.     }
  1084. #endif /* DEBUG_INFO */
  1085. }
  1086.  
  1087.  
  1088. /*
  1089.  * Can the process "p" access the "nbytes" long
  1090.  * block of memory starting at "start"?
  1091.  * If it would be a legal access, the current
  1092.  * process is given temporary access via
  1093.  * prot_temp.
  1094.  * Returns a cookie like the one prot_temp
  1095.  * returns; if the process shouldn't have
  1096.  * access to the memory, returns 1.
  1097.  *
  1098.  * BUG: should actually read p's page table to
  1099.  * determine access
  1100.  */
  1101.  
  1102. int
  1103. mem_access_for(p, start, nbytes)
  1104.     PROC *p;
  1105.     ulong start;
  1106.      long nbytes;
  1107. {
  1108.     MEMREGION **mr;
  1109.     int i;
  1110.  
  1111.     if (no_mem_prot) return -1;
  1112.     if (start >= (ulong)p && start+nbytes <= (ulong)(p+1))
  1113.         return -1;
  1114.     if (p == rootproc)
  1115.         goto win_and_mark;
  1116.  
  1117.     mr = p->mem;
  1118.     if (mr) {
  1119.         for (i = 0; i < p->num_reg; i++, mr++) {
  1120.         if (*mr) {
  1121.             if (((*mr)->loc <= start) &&
  1122.             ((*mr)->loc + (*mr)->len >= start + nbytes))
  1123.                 goto win_and_mark;
  1124.         }
  1125.         }
  1126.     }
  1127.  
  1128.     return 0;    /* we don't own this memory */
  1129.  
  1130. win_and_mark:
  1131.     return prot_temp(start, nbytes, -1);
  1132. }
  1133.